$reffile = "./EQTL_NEW/inputfiles/GBM/file_final_ucsc_new_uniq";
#$reffile = $ARGV[0];
chomp($reffile);
#$tpedfile = $ARGV[2];
$tpedfile = "combined_GBM_normal_final_hthoLT15_out.tped";
chomp($tpedfile);
#$expr = $ARGV[1];
$expr ="merge_expr_out";
chomp($expr);
open(BUFF,$reffile);
$line = <BUFF>;
while($line = <BUFF>)
{
	chomp($line);
	@array = split(/\t/,$line);
	$array[5] =~ s/M/26/g;
	$array[5] =~ s/X/23/g;
	$array[5] =~ s/Y/24/g;
	#print $array[5]."\n";
	$array[5] =~ s/_random//g;
	if($array[5] =~ m/random/ || $array[5] eq "null" || $array[5] eq "" || $array[5] eq " ")
	{
	}
	else
	{
		$hash{$array[0]} = $array[3]."_".$array[4]."_".$array[5];
		#print $array[5]."\n";	
	}
	#print $array[5]."\n";
	#print $hash{$array[0]}."\n";
}

open(BUFF1,$expr);
open(WRB,">temp_ref_file");
#print $expr."\n";
$ann_expr = <BUFF1>;
while($line = <BUFF1>)
{
        chomp($line);
	#print $line."\n";
	@array = split(/\t/,$line);
	if(exists($hash{$array[0]}))
	{
		#print "succss\n";
		#$hash1{$array[0]} = $array[1];
		#print $hash{$array[0]}."\n";
		$li = $hash{$array[0]};
		@arr = split('_',$li);
		#$gene = $array[0];
		$gene = shift(@array);
		#print $gene."\n";
		$line = join(" ",@array);
		$chr = $arr[2];
		#$start = $arr[0];
		$start = $arr[0]-10000;
		if($start < 0)
		{
			$start =0;
		}
		#$stop = $arr[1];
		$stop = $arr[1]+10000;
		$hash2{$chr} = 1;
		#print WRB $gene."\t".$chr."\t".$start."\t".$stop."\t".$array[1]."\n";	
		#print WRB $gene."\t".$chr."\t".$start."\t".$stop."\t".$line."\n";
		print WRB $gene."\t".$chr."\t".$start."\t".$stop."\n";
	}
	else
	{
		#print $line."\n";
	}
}
system("sort -k2,2n  -k3,3n -k4,4n -k1,1n temp_ref_file > result_ref_file");
system("rm temp_ref_file");
open(REF,"result_ref_file");
open(GNT,"$tpedfile");
open(GNT1,"$tpedfile");
open(WRBUFF,">outfile");
for($i=1;$i<27;$i++)
{
	${"nu$i"} = 0;
}
while($line = <REF>)
{
        chomp($line);
        $line =~ s/\t/ /g;
	#print $line."\n";
        @array = split(/ /,$line);
        $chr1 = $array[1];
	$pos1_start = $array[2];
        $pos1_stop = $array[3];
	chomp($chr1);
	#print "$chr1\n";
	${"gene_info$chr1"}[${"nu$chr1"}] = $array[0]."_"."start_$chr1";
	${"gene_info_pos$chr1"}[${"nu$chr1"}] =$pos1_start;
	if($chr1 == 1){
	#print $chr1."\t".${"nu$chr1"}."\t".${"gene_info$chr1"}[${"nu$chr1"}]."\t".${"gene_info_pos$chr1"}[${"nu$chr1"}]."\n";
	}
	${"nu$chr1"}++;
	${"gene_info$chr1"}[${"nu$chr1"}] = $array[0]."_"."stop_$chr1";
	${"gene_info_pos$chr1"}[${"nu$chr1"}] =$pos1_stop;
	if($chr1 == 1){
	#print $chr1."\t".${"nu$chr1"}."\t".${"gene_info$chr1"}[${"nu$chr1"}]."\t".${"gene_info_pos$chr1"}[${"nu$chr1"}]."\n";
	}
	${"nu$chr1"}++;
	#$gene = shift(@array);
	# shift(@array);
	# shift(@array);
	 #shift(@array);
	#$line = join("_",@array);
	#$expr{$gene} = $line; 
}
#print "test".@gene_info1."\n";
$knu = 0;
#$j =1;
for($j=1;$j<25;$j++)
{
	

	@index = ();
	@index = sort {${"gene_info_pos$j"}[$a] <=> ${"gene_info_pos$j"}[$b]}  0 .. $#{"gene_info_pos$j"};
	#print "length ".@index."\n";
	for($i=0;$i<@index;$i++)
	{
		$gene_info_1[$knu] = ${"gene_info$j"}[$index[$i]];
		$gene_info_pos_1[$knu] = ${"gene_info_pos$j"}[$index[$i]];
		#print $j."\t".$index[$i]."\t".$gene_info_1[$knu]."\t".$gene_info_pos_1[$knu]."\t".${"gene_info$j"}[$i]."\t".${"gene_info_pos$j"}[$i]."\n";
		#print $j."\t".$i."\t".${"gene_info$j"}[$i]."\t".${"gene_info_pos$j"}[$i]."\n";
		$knu++;
	}
}
$prevchr = "";
$nun = 0;
#coding start
$ann_tped = <GNT>;
print WRBUFF $ann_tped;
$tped = <GNT>;
@arr = split(" ",$tped);
$chr2 = shift(@arr);
$rsid = shift(@arr);
shift(@arr);
$pos2 = shift(@arr);
$tped = join(" ",@arr);
$tped =$rsid." $tped";
for($i=0;$i<@gene_info_1;$i++)
{
	@garray = split('_',$gene_info_1[$i]);
	$chr1 = $garray[2];
	$pos = $gene_info_pos_1[$i];
	$flag =0;
	if($nun != 0 && $chr1 != $chr2)
	{
		while(($key,$value)= each %hs)
		{
			chomp($hs{$key});
			@testarr = split(/\t/,$hs{$key});
			if(@testarr > 1)
			{
				print WRBUFF $hs{$key}."\n";
			}
			delete($hs{$key});
		}
	}
	$nun++;
	if($gene_info_1[$i] =~ m/stop/ && $chr1 == $chr2)
	{
		$flag =1;
		#print $gene_info_1[$i]."\n";
		if(exists($hs{$garray[0]}))
		{
			chomp($hs{$garray[0]});
                        @testarr = split(/\t/,$hs{$garray[0]});
                        if(@testarr > 1)
                        {

			print WRBUFF $hs{$garray[0]}."\n";
			}
			delete($hs{$garray[0]});
		}
	}
	
	while($chr1 < $chr2 && $chr1 ne "" && $chr2 ne "")
        {
		$i++;
		@garray = split('_',$gene_info_1[$i]);
        	$chr1 = $garray[2];
        	$pos = $gene_info_pos_1[$i];
		$prevchr = $chr1;
	}
        while($chr1 > $chr2 && $chr1 ne "" && $chr2 ne "")
        {
		$tped = <GNT>;
                @arr = split(" ",$tped);
                $chr2 = shift(@arr);
                $rsid = shift(@arr);
                shift(@arr);
                $pos2 = shift(@arr);
                $tped = join(" ",@arr);
		$tped =$rsid." $tped";
        }
	if($chr1 == $chr2 && $flag ==0)
	{
		
		while($pos > $pos2 && $chr1 == $chr2)
		{
			$tped = <GNT>;
                	@arr = split(" ",$tped);
                	$chr2 = shift(@arr);
                	$rsid = shift(@arr);
                	shift(@arr);
                	$pos2 = shift(@arr);
                	$tped = join(" ",@arr);
			$tped =$rsid." $tped";
		}
		@garray_next = split('_',$gene_info_1[$i+1]);
		$pos_next = $gene_info_pos_1[$i+1];
		if($chr2 == $garray_next[2])
		{
			if(!(exists($hs{$garray[0]}))) 
			{
				#$hs{$garray[0]} = $garray[0]."\t".$expr{$garray[0]}."\t";
				$hs{$garray[0]} = $garray[0];
			}
		}
		while($pos <= $pos2 && $pos2 <= $gene_info_pos_1[$i+1] && $chr1 == $chr2)
		{
			if($gene_info_1[$i] =~ m/start/ )
                	{
				if(!(exists($hs{$garray[0]})))
				{
                        		#$hs{$garray[0]} = $garray[0]."\t".$expr{$garray[0]}."\t";
					
					$hs{$garray[0]} = $garray[0];
					#print $garray[0]."\t".$expr{$garray[0]}."\t";
				}
                	}
	
			while(($key,$value)=each %hs)
			{
				$hs{$key} =  $hs{$key}."\t$tped";	
			}
			$tped = <GNT>;
                        @arr = split(" ",$tped);
                        $chr2 = shift(@arr);
                        $rsid = shift(@arr);
                        shift(@arr);
                        $pos2 = shift(@arr);
                        $tped = join(" ",@arr);
			$tped =$rsid." $tped";
                        $tped1 = <GNT1>;
                        @arr1 = split(" ",$tped1);
                        $chr21 = shift(@arr1);
                        shift(@arr1);
                        shift(@arr1);
                        $pos21 = shift(@arr1);
	

		}
	}
	$prevchr = $chr1;
}
#print WRBUFF $ann_tped;
#print "empty $ann_tped";
while(($key,$value)= each %hs)
{
	 chomp($hs{$key});
                        @testarr = split(/\t/,$hs{$key});
                        if(@testarr > 1)
                        {
                                print WRBUFF $hs{$key}."\n";
                        }
	
	delete($hs{$key});
}

close(WRBUFF);
open(BUG,$expr);
$line_in=<BUG>;
while($line = <BUG>)
{
        chomp($line);
        @array = ();
        @array =split("\t",$line);
	$hash_expr{$array[0]} = $line;
}
open(BUG,"outfile");
$line=<BUG>;
open(WRBUFF,">outfile_expr");
print WRBUFF $line_in;
while($line = <BUG>)
{
        chomp($line);
        @array = ();
         @array =split("\t",$line);
        print WRBUFF $hash_expr{$array[0]}."\n";
}
close(BUG);
close(WRBUFF);
